package xfsale.service.impl.j2ee;

import java.sql.PreparedStatement;
import java.sql.ResultSet;

import xfsale.packet.TransAcctPassSetRequest;
import xfsale.packet.TransAcctPassSetResponse;

public final class TransAcctPassSet extends BaseOrderAcct {

	private TransAcctPassSetRequest request;
	private TransAcctPassSetResponse response;

	public TransAcctPassSet(TransAcctPassSetRequest request, TransAcctPassSetResponse response) {
		super(request, response);
		this.request = request;
		this.response = response;
		response.returnCode = "0000";
		response.returnMsg = "交易账户密码重置成功";
	}

	//检查输入参数
    @Override
	protected void checkData() throws Exception {
		super.checkData();

		checkTradePassword(request.tradePasswordNew);
	}

	//初始化
    @Override
	protected void load() throws Exception {
		super.load();
	}

	//检查功能权限
    @Override
	protected void checkPriv() throws Exception {
		super.checkPriv();
	}

	//检查条件是否满足
    @Override
	protected void checkCondition() throws Exception {
		super.checkCondition();
	}

	//功能处理
    @Override
	protected void process() throws Exception {
		//重置交易账户密码
		resetPassword();

		//写入账户委托数据
		writeOrderAcct();
	}

	//重置交易账户密码
	private void resetPassword() throws Exception {
		PreparedStatement stmt = null;
		try {
			logger.debug("重置交易账户密码...");
			stmt = conn.prepareStatement("" +
				"update AcctTrans set " +
				"tradePassword = ? " +
				"where transAccountID = ? "
			);
			stmt.setString(1, request.tradePasswordNew);
			stmt.setString(2, request.transAccountID);
			stmt.executeUpdate();
			logger.debug("重置交易账户密码成功.");
		} catch (Exception e) {
			logger.error("重置交易账户密码出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}


	//写入账户委托数据
	private void writeOrderAcct() throws Exception {
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			//生成委托流水
			logger.debug("生成委托流水...");
			serial.setTradeDate(sys.getWorkDay());
			serial.genOrderSerial();
			response.orderSerialNo = serial.getOrderSerialNo();

			//写入账户委托数据
			logger.debug("写入账户委托数据...");
			stmt = conn.prepareStatement(SQL_WRITE_ORDER_ACCT);
			stmt.setString(1, response.orderSerialNo);      //委托流水
            stmt.setString(2, request.serviceCode);         //功能代码
            stmt.setString(3, businessCode);                //业务码
            stmt.setString(4, request.acceptMethod);        //受理方式
            stmt.setString(5, request.regionCode);          //地区编号
            stmt.setString(6, request.branchCode);          //网点编号
            stmt.setString(7, request.netCode);             //销售网点编号
            stmt.setString(8, request.opCode);              //柜员编号
            stmt.setString(9, custAccountID);               //客户帐号
            stmt.setString(10, request.transAccountID);     //交易帐号
            stmt.setString(11, null);                       //基金帐号
            stmt.setString(12, null);                       //TA代码
            stmt.setString(13, custType);                   //客户类型
            stmt.setString(14, custName);                   //客户名称
            stmt.setString(15, custNameAbbr);               //客户简称
            stmt.setString(16, nationality);                //国籍
            stmt.setString(17, certType);                   //证件类型
            stmt.setString(18, certNo);                     //证件号码
            stmt.setString(19, certValidFrom);              //证件有效期起始日期
            stmt.setString(20, certValidTo);                //证件有效期截至日期
            stmt.setString(21, null);                       //上交所证券帐号
            stmt.setString(22, null);                       //深交所证券帐号
            stmt.setString(23, null);                       //通讯地址
            stmt.setString(24, null);                       //邮政编码
            stmt.setString(25, null);                       //电话号码
            stmt.setString(26, null);                       //住址电话
            stmt.setString(27, null);                       //办公电话
            stmt.setString(28, null);                       //手机号码
            stmt.setString(29, null);                       //传真号码
            stmt.setString(30, null);                       //E-MAIL地址
            stmt.setString(31, null);                       //出生日期
            stmt.setString(32, null);                       //性别
            stmt.setString(33, null);                       //教育程度
            stmt.setString(34, null);                       //职业代码
            stmt.setString(35, null);                       //行业代码
            stmt.setString(36, null);                       //婚姻状况
            stmt.setString(37, null);                       //年收入
            stmt.setString(38, null);                       //机构类型
            stmt.setString(39, null);                       //机构性质
            stmt.setString(40, null);                       //机构注册地
            stmt.setString(41, null);                       //机构注册资本
            stmt.setString(42, null);                       //机构员工数量
            stmt.setString(43, null);                       //法人姓名
            stmt.setString(44, null);                       //法人证件类型
            stmt.setString(45, null);                       //法人证件号码
            stmt.setString(46, null);                       //法人证件有效期起始日期
            stmt.setString(47, null);                       //法人证件有效期截至日期
            stmt.setString(48, request.instTransName);              //经办人姓名
            stmt.setString(49, request.instTransCertType);          //经办人证件类型
            stmt.setString(50, request.instTransCertNo);            //经办人证件号码
            stmt.setString(51, request.instTransCertValidFrom);     //经办人证件有效期起始日期
            stmt.setString(52, request.instTransCertValidTo);       //经办人证件有效期截至日期
            stmt.setString(53, null);                               //经办人联系电话
            stmt.setString(54, null);                               //经办人移动电话
            stmt.setString(55, null);                               //经纪人
            stmt.setString(56, null);                       //银行代码
            stmt.setString(57, null);                       //银行地址
            stmt.setString(58, null);                       //银行全称
            stmt.setString(59, null);                       //银行帐号户名
            stmt.setString(60, null);                       //银行帐号卡号
            stmt.setString(61, null);                       //银行账号有效期
            stmt.setString(62, null);                       //银行帐号验证
            stmt.setString(63, null);                       //对帐单寄送选择
            stmt.setString(64, null);                       //对账单寄送方式
            stmt.setString(65, null);                       //交易方式
            stmt.setString(66, null);                       //风险承受能力
            stmt.setString(67, null);                       //冻结截至日期
            stmt.setString(68, null);                       //冻结原因
            stmt.setString(69, null);                       //原委托流水
            stmt.setString(70, null);                       //原申请流水
            stmt.setString(71, "0");                        //委托预约标志
            stmt.setString(72, null);                       //委托预约日期
            stmt.setString(73, "0");                        //是否需要授权
            stmt.setString(74, null);                       //授权标志(0-未授权 1-授权通过 2-授权不通过)
            stmt.setString(75, null);                       //授权操作员
            stmt.setString(76, null);                       //授权日期
            stmt.setString(77, null);                       //授权时间
            stmt.setString(78, "1");                        //委托状态
            stmt.setString(79, sys.getWorkDay());           //委托日期
            stmt.setString(80, sys.getWorkTime());          //委托时间
            stmt.setString(81, null);                       //备注
			stmt.executeUpdate();
			logger.debug("写入账户委托数据成功.");
		} catch (Exception e) {
			logger.error("写入账户委托数据出错. " + e);
			throw e;
		} finally {
			stmt.close();
		}
	}

}